package org.mspring.mlog.action.admin;

import org.mspring.mlog.TabItem;
import org.mspring.mlog.entity.Link;
import org.mspring.mlog.entity.User;
import org.mspring.mlog.service.LinkService;
import org.mspring.nbee.common.utils.RequestUtils;
import org.mspring.nbee.orm.dao.sql.builder.part.condition.Operator;
import org.mspring.nbee.orm.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.orm.dao.sql.pager.OrderType;
import org.mspring.nbee.orm.dao.sql.pager.PageResult;
import org.mspring.nbee.orm.dao.sql.pager.Pager;
import org.mspring.nbee.web.Action;
import org.mspring.nbee.web.exception.OperateException;
import org.mspring.nbee.web.support.QueryParam;
import org.mspring.nbee.web.support.WebContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-24 13:13
 */
@Action
@RequestMapping("/admin/link")
@TabItem(item = "200110")
public class LinkAction extends BaseAdminAction {
    private LinkService linkService;

    @RequestMapping("/list")
    @TabItem(item = "200110005")
    public String list(WebContext context, Model model) {
        Pager pager = context.getPager();
        pager.addOrder("id", OrderType.DESC);
        List<SqlCondition> conditions = context.buildSqlConditions(
                model,
                new QueryParam("name", Operator.LIKE)
        );
        PageResult<Link> result = linkService.list(conditions, pager);
        model.addAttribute("result", result);
        return "/admin/link/list";
    }

    @RequestMapping("/create")
    @TabItem(item = "200110010")
    public String create(WebContext context, Model model) {
        return "/admin/link/create";
    }

    @RequestMapping("/create/do")
    public String doCreate(WebContext context, Model model) {
        User currentUser = securityService.getCurrentUser(context.getRequest());
        Link link = context.fromRequest(Link.class);
        link.setCreateTime(new Date());
        link.setCreateUser(currentUser.getId());
        link.setCreateUserName(currentUser.getNickname());
        link.setUpdateTime(new Date());
        link.setUpdateUser(currentUser.getId());
        link.setUpdateUserName(currentUser.getNickname());
        linkService.insert(link);
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/edit")
    @TabItem(item = "200110015")
    public String edit(WebContext context, Model model) {
        Long id = RequestUtils.getParamAsLong(context.getRequest(), "id");
        if (id == null) {
            throw new OperateException("操作异常：请选择要编辑的链接");
        }
        Link link = linkService.findById(id);
        model.addAttribute("link", link);
        return "/admin/link/edit";
    }

    @RequestMapping("/edit/do")
    public String doEdit(WebContext context, Model model) {
        User currentUser = securityService.getCurrentUser(context.getRequest());
        long id = RequestUtils.getRequiredLong(context.getRequest(), "id");
        Link link = linkService.findById(id);
        link = context.fromRequest(link);
        link.setUpdateTime(new Date());
        link.setUpdateUser(currentUser.getId());
        link.setUpdateUserName(currentUser.getNickname());
        linkService.update(link);
        return "redirect:/admin/link/list";
    }

    @RequestMapping("/delete/do")
    public void delete(WebContext context, Model model) {
        long id = RequestUtils.getRequiredLong(context.getRequest(), "id");
        boolean flag = linkService.deleteById(id);
        context.renderJsonResult(flag);
    }

    @Autowired
    public void setLinkService(LinkService linkService) {
        this.linkService = linkService;
    }
}
